package com.kk.transactional.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kk.transactional.entity.Person;
import com.kk.transactional.mapper.PersonMapper;
import com.kk.transactional.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author little_lunatic
 * @since 2025-06-05
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements IPersonService {


    @Autowired
    PersonMapper personMapper;


    @Transactional(propagation = Propagation.REQUIRED)
    public void add() {
        Person person = new Person();
        person.setName("test");
        this.save(person);
        throw new RuntimeException();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void add2() {
        Person person = new Person();
        person.setName("SUPPORTS");
        this.save(person);
        throw new RuntimeException();
        
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void add3() {
        Person person = new Person();
        person.setName("MANDATORY");
        this.save(person);
        
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void add4() {
        Person person = new Person();
        person.setName("REQUIRES_NEW");
        this.save(person);
        
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void add44() {
        Person person = new Person();
        person.setName("REQUIRES_NEW44");
        this.save(person);
        throw new RuntimeException();

    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void add5() {
        Person person = new Person();
        person.setName("NOT_SUPPORTED");
        this.save(person);  // 没有事务，不会回滚
        
    }

    @Transactional(propagation = Propagation.NEVER)
    public void add6() {
        Person person = new Person();
        person.setName("NEVER");
        this.save(person);
        
    }

    @Transactional(propagation = Propagation.NESTED)
    public void add7() {
        Person person = new Person();
        person.setName("NESTED");
        this.save(person);
        
    }

    @Transactional(propagation = Propagation.NESTED)
    public void add8() {
        Person person = new Person();
        person.setName("NESTED2");
        this.save(person);
        throw new RuntimeException();
    }
}
